import axios from "axios";
import config from "@/config";
axios.defaults.headers["Content-Type"] = "application/json;charset=utf-8";
// 创建axios实例
const service = axios.create({
  // axios中请求配置有baseURL选项，表示请求URL公共部分
  baseURL: config.baseApi,
  // 超时
  timeout: 100000
});
// request拦截器
service.interceptors.request.use(
  config => {
    return config;
  },
  error => {
    Promise.reject(error);
  }
);

// 响应拦截器
service.interceptors.response.use(
  res => {
    const code = res.data.code;
    if (code !== 200) {
      return Promise.reject("error");
    } else {
      return res.data;
    }
  },
  error => {
    return Promise.reject(error);
  }
);

export default service;
